﻿
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using System.Web;

namespace Module.Core.Utilities
{
   public class ConvertImage
    {
       public static string SaveImages(string filePath)
       {
           FileInfo fileInfo = new FileInfo(filePath);
           string _temPath = "/ThumbImages/" + DateTime.Now.Year + "/" + DateTime.Now.Month.ToString("d2") + "/" +
                                 DateTime.Now.Day.ToString("d2") + "/";
           string newPath = System.Web.HttpContext.Current.Server.MapPath(_temPath);
           if (!Directory.Exists(newPath)) Directory.CreateDirectory(newPath);
           string FileSave = "";
           string name = "";
           if (filePath.Length > 0)
           {
               name = fileInfo.Name;
               FileSave = newPath + name;
               if (!File.Exists(FileSave))
               {
                   Image bitmap = Image.FromFile(filePath);
                   bitmap.Save(FileSave);
               }
           }
           return _temPath + name;
       }

       public static void resizeImage(string fromPath, int size, bool dimension, string desPath) // resize theo 1 chieu
       {
           Image fullSizeImage = Image.FromFile(fromPath);
           int width = fullSizeImage.Width;
           int height = fullSizeImage.Height;
           float fromScale = (float)width / (float)height;

           if (dimension == true) // resize theo width
           {
               width = size;
               height = (int)((float)width / fromScale);
           }
           else // resize theo height
           {
               height = size;
               width = (int)(fromScale * (float)height);
           }
           resize(fullSizeImage, width, height, desPath);
           fullSizeImage.Dispose();
       }

       private static void resize(Image fullSizeImage, int width, int height, string desPath)
       {
           try
           {
               Bitmap result = new Bitmap(width, height, PixelFormat.Format32bppRgb);
               result.SetResolution(72, 72);

               Graphics g = Graphics.FromImage((Image)result);
               g.CompositingQuality = CompositingQuality.HighQuality;
               g.SmoothingMode = SmoothingMode.AntiAlias;
               g.InterpolationMode = InterpolationMode.HighQualityBicubic;
               g.PixelOffsetMode = PixelOffsetMode.HighQuality;
               g.DrawImage(fullSizeImage, 0, 0, width, height);

               EncoderParameters codecParams = new EncoderParameters(1);
               codecParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)95);

               ImageCodecInfo[] encoders;
               encoders = ImageCodecInfo.GetImageEncoders();
               ImageCodecInfo ici = null;
               foreach (ImageCodecInfo codec in encoders)
                   if (codec.MimeType == "image/jpeg")
                       ici = codec;
               if (!File.Exists(desPath))
                   result.Save(desPath, ici, codecParams);
               result.Dispose();
               g.Dispose();
           }
           finally
           { }
       }

       public static string SetAvatarThumbnailWidthDirection(string filePath, int iThumbWidth, string _suffix)
       {
           FileInfo fileInfo = new FileInfo(filePath);
           try
           {
               string _temPath = "/ThumbImages/" + DateTime.Now.Year + "/" + DateTime.Now.Month.ToString("d2") + "/" + DateTime.Now.Day.ToString("d2") + "/";
               string newPath =HttpContext.Current.Server.MapPath(_temPath);
               if (!Directory.Exists(newPath)) Directory.CreateDirectory(newPath);
               string name = fileInfo.Name.Replace(fileInfo.Extension, "").Replace(_suffix.ToString().ToLower(), "");//tên ảnh
               string newfileName = newPath + name + _suffix.ToString().ToLower() + ".jpg";  //  ThumbImages/2010/03/12/aaa.jpg                
               if (!File.Exists(newfileName))
               {
                   resizeImage(filePath, iThumbWidth, true, newfileName);
               }
               return _temPath + name + _suffix.ToString().ToLower() + ".jpg";
           }
           finally
           { }
       }
    }
}
